package com.gec.controller;

import com.gec.domain.Role;
import com.gec.domain.User;
import com.gec.exception.AuthenticateException;
import com.gec.service.abst.UserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/Common")
public class CommonController extends BaseController {
    private final UserService userService;

    public CommonController(UserService userService) {
        this.userService = userService;
    }

    @RequestMapping("/welcome")
    public String Welcome() {
        // 内部转发到 /WEB-INF/jsp/welcome.jsp
        return "welcome";
    }

    @RequestMapping("/tempIndex")
    public String tempIndex() {
        //内部转发: /WEB-INF/jsp/index_temp.jsp
        return "index_temp";
    }

    private final DB db = new DB();

    @RequestMapping("/login_temp")
    public String login_temp(String account, HttpSession session) {
        User user;
        System.out.println(account);
        try {
            user = db.getUser(account);
            session.setAttribute("user", user);
            System.out.println("+------------Login----------------");
            System.out.println(user);
            System.out.println(user.getPrimaryRole());
            System.out.println(user.getDept());
            System.out.println("+------------Login----------------");
            return "redirect:/Common/tempIndex";
        } catch (AuthenticateException e) {
            e.printStackTrace();
            return "redirect:/Error";
        }
    }

    @RequestMapping("/showLogin")
    public String showLogin() {
        //内部转发: /WEB-INF/jsp/login.jsp
        return "login";
    }

    @RequestMapping("/index")
    public String index() {
        //内部转发: /WEB-INF/jsp/index.jsp
        return "index";
    }

    //{ps}登录数据库访问不在这个方法中编写
    //    转移到 Realm 中处理了。
    @RequestMapping("/login")
    public String login(
            @RequestParam("account") String account,
            @RequestParam("password") String password) {
        //{1}创建一个令牌 Token
        UsernamePasswordToken token = new UsernamePasswordToken(
                account, password);
        //{2}获取到主体对象 [门面][与安全管理器交互]
        Subject subject = SecurityUtils.getSubject();
        try {
            //{3}传入令牌做用户身份认证。
            //   认证过程中会抛出异常。
            subject.login(token);
            //{4}把用户对象设置到会话域。
            Session session = subject.getSession();
            //{5}你在 realm 中返回一个认证信息类到认证器后。
            //    从 subject 取出的身份就是 user 对象
            //    (你传进去的对象。)
            User user = (User) subject.getPrincipal();
            session.setAttribute("user", user);

            //{6}设置 MENU_KEY [菜单钥匙]
            Role role = user.getPrimaryRole();
            String MENU_KEY = "MENU_" + role.getId();
            session.setAttribute("MENU_KEY", MENU_KEY);
            //{7}重定向到主页。
            return "redirect:/Common/index";

        } catch (Exception e) {
            e.printStackTrace();
            String msg = "用户登录失败.";
            msg = makeURLMsg(msg);
            return "redirect:/Common/showLogin?error=" + msg;
        }
    }


    @RequestMapping(
            value = "/unauthorized",
            produces = "text/html;charset=UTF-8"
    )
    @ResponseBody
    public String unauthorized() {
        return "你没有这个权限访问资源";
    }

    @RequestMapping("/logout")
    public String logout(HttpServletRequest req) {
        HttpSession session = req.getSession();
        session.setAttribute("user", null);
        session.setAttribute("MENU_KEY", null);
        return "login";
    }

    @RequestMapping("/level")
    public String level() {
        return "error";
    }
}
